﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QMSDAL;
using System.Data;

namespace QuaManSys.InnerInfo
{
    public partial class InnerInfoQueryList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDropDownLists();    //采用数据库中的数据，初始化下拉框
            }
        }

        /// <summary>
        /// 初始化下拉框
        /// </summary>
        private void InitDropDownLists()
        {
            InitProNos();   //初始化工程编号
            InitMachineType();  //初始化机型
            InitMachineNos();   //初始化机号
        }

        //加载所有机号，默认项为空
        private void InitMachineNos()
        {
            //清空后加载默认项
            ddlMacNos.Items.Clear();
            ListItem defaultItem = new ListItem("----请选择----", "-1");
            ddlMacNos.Items.Add(defaultItem);

            //加载数据库中的数据
            DataSet ds = GCBH.GetAllTF();
            foreach (DataTable dt in ds.Tables)
            {
                for (int culCol = 0; culCol < dt.Columns.Count; culCol++)
                {
                    for (int culRow = 0; culRow < dt.Rows.Count; culRow++)
                    {
                        ListItem li = new ListItem(dt.Rows[culRow][culCol].ToString());
                        ddlMacNos.Items.Add(li);
                    }
                }
            }

        }

        //加载所有机型，默认项为空
        private void InitMachineType()
        {
            //清空后加载默认项
            ddlMacType.Items.Clear();
            ListItem defaultItem = new ListItem("----请选择----", "-1");
            ddlMacType.Items.Add(defaultItem);

            //加载数据库中的数据
            DataSet ds = GCBH.GetAllJX();
            foreach (DataTable dt in ds.Tables)
            {
                for (int culCol = 0; culCol < dt.Columns.Count; culCol++)
                {
                    for (int culRow = 0; culRow < dt.Rows.Count; culRow++)
                    {
                        ListItem li = new ListItem(dt.Rows[culRow][culCol].ToString());
                        ddlMacType.Items.Add(li);
                    }
                }
            }
        }

        //加载所有机号，默认项为空
        private void InitProNos()
        {
            //清空后加载默认项
            ddlProNo.Items.Clear();
            ListItem defaultItem = new ListItem("----请选择----", "-1");
            ddlProNo.Items.Add(defaultItem);

            //加载数据库中的数据
            DataSet ds = GCBH.GetAllProjectNumbers();
            foreach (DataTable dt in ds.Tables)
            {
                for (int culCol = 0; culCol < dt.Columns.Count; culCol++)
                {
                    for (int culRow = 0; culRow < dt.Rows.Count; culRow++)
                    {
                        ListItem li = new ListItem(dt.Rows[culRow][culCol].ToString());
                        ddlProNo.Items.Add(li);
                    }
                }
            }
        }

        /// <summary>
        /// 总表查询
        /// </summary>
        /// <returns></returns>
        public List<InnerQualityInfo> GetAllProducts()
        {
            List<InnerQualityInfo> results = new List<InnerQualityInfo>();
            using (var entity = new QuaManSysEntities())
            {
                var queryAll = from q in entity.InnerQualityInfo
                               select q;
                results = queryAll.ToList();
            }
            return Fileter(results);    //按所选条件筛选信息
        }

        /// <summary>
        /// 按条件筛选需要的信息
        /// </summary>
        /// <param name="results"></param>
        /// <returns></returns>
        private List<InnerQualityInfo> Fileter(List<InnerQualityInfo> results)
        {
            //根据信息表编号筛选
            string infoId = txtInfoId.Text.Trim();
            if (infoId != "")
                results = (from q in results
                           where q.qualityInfoId == infoId
                           select q).ToList();

            //根据工程编号筛选
            string proNo = ddlProNo.Text;
            if (proNo != "-1")  //-1为默认值
                results = (from q in results
                           where q.projectNo == proNo
                           select q).ToList();

            //根据机型来筛选
            string macType = ddlMacType.Text;
            if (macType != "-1")
            {
                List<InnerQualityInfo> res = new List<InnerQualityInfo>();
                List<string> listNos = GCBH.GetGCBHbyJX(macType);
                foreach (var item in listNos)
                {
                    var query = from q in results
                                where q.projectNo == item
                                select q;
                    res.AddRange(query.ToList());
                }
                results = res;
            }

            //根据机号来筛选
            string macNo = ddlMacNos.Text;
            if (macNo != "-1")
            {
                List<InnerQualityInfo> res = new List<InnerQualityInfo>();
                List<string> listNos = GCBH.GetGCBHbyTF(macNo);
                foreach (var item in listNos)
                {
                    var query = from q in results
                                where q.projectNo == item
                                select q;
                    res.AddRange(query.ToList());
                }
                results = res;
            }

            //根据重要级别来筛选
            string impLevel = ddlImpLev.Text;
            if (impLevel != "")
                results = (from q in results
                           where q.levelOfImportance == impLevel
                           select q).ToList();

            //根据信息表状态筛选
            string infoState = ddlInfoState.Text;
            if (infoState != "")
                results = (from q in results
                           where q.processingStatus == infoState
                           select q).ToList();

            //将未处理的信息放前面
            results = (from q in results
                       orderby q.projectCategory
                       select q).ToList();

            List<InnerQualityInfo> finalResults = results;
            return finalResults;
        }


        /// <summary>
        /// 部门查询
        /// </summary>
        /// <returns></returns>
        public List<InnerQualityInfo> GetProducts()
        {
            List<InnerQualityInfo> results = new List<InnerQualityInfo>();
            using (var entity = new QuaManSysEntities())
            {
                var queryAll = from q in entity.InnerQualityInfo
                               select q;
                results = queryAll.ToList();
            }

            CurUser user = (CurUser)Session["CurUser"];
            List<InnerQualityInfo> ret = new List<InnerQualityInfo>();
            foreach (var item in results)
            {
                List<string> relatedDept = Common.InnerRelatedDeptOperation.QueryInnerRelatedDept(item.qualityInfoId);
                if (relatedDept.Contains(user.department))
                {
                    ret.Add(item);
                }
            }
            return Fileter(ret);    //按所选条件筛选信息

        }

        //总表查询
        protected void btnSearchAll_Click(object sender, EventArgs e)
        {
            CurUser user = (CurUser)Session["CurUser"];
            if (Permission.JudgePermission(user, "1C", "E0"))
            {
                List<InnerQualityInfo> attributes = GetAllProducts();
                lblCount.Text = Convert.ToString(attributes.Count());
                GridView1.DataSource = attributes;
                GridView1.DataBind();
            }
            else
            {
                lblCount.Text = "0";
                GridView1.DataSource = null;
                GridView1.DataBind();

                Response.Write("<Script>alert('权限不足，无法操作！');location.href='InnerInfoQueryList.aspx';</Script>");
            }

        }

        private Exception Exception(string p)
        {
            throw new NotImplementedException();
        }
        //分表查询
        protected void btnSearchByDept_Click(object sender, EventArgs e)
        {
            CurUser user = (CurUser)Session["CurUser"];
            if (Permission.JudgePermission(user, "1C", "E1"))
            {
                List<InnerQualityInfo> attributes = GetProducts();
                lblCount.Text = Convert.ToString(attributes.Count());
                GridView1.DataSource = attributes;
                GridView1.DataBind();
            }
            else
            {
                lblCount.Text = "0";
                GridView1.DataSource = null;
                GridView1.DataBind();
                Response.Write("<Script>alert('权限不足，无法操作！');location.href='InnerInfoQueryList.aspx';</Script>");
            }

        }
    }
}